<?php
namespace CTCF;
class Model{
	public $configs = array();
	public $M;
	public $table="";
	public function __construct($table = ""){
		$this->configs = include(APP.'/Conf/db.config.php');
		include_once './Core/Lib/CTCF/medoo.class.php';
		$this->M = new \medoo([
				'database_type' => $this->configs['db_type'],
				'database_name' => $this->configs['db_name'],
				'server' => $this->configs['db_host'],
				'username' => $this->configs['db_username'],
				'password' => $this->configs['db_pwd'], 
				// optional
				'port' => $this->configs['db_port'],
				'charset' => $this->configs['db_charset'],
				//sqlite
				'database_file' => $this->configs['db_file'],
				// driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
				'option' => [
					\PDO::ATTR_CASE => \PDO::CASE_NATURAL
				]]);
		$this->table = $table==""?"":$this->configs['db_tb_pre'].$table;
	}
	
	public function select($join,$columns = null, $where = null){
		return $this->M->select($this->table, $join,$columns, $where);
	}	
	public function query($query){
		return $this->M->query($query);
	}
	public function exec($query){
		return $this->M->exec($query);		
	}
	public function quote($string)
	{
		return $this->M->quote($string);
	}
	public function insert($datas)
	{
		return $this->M->insert($this->table, $datas);
	}
	public function update($data, $where = null){
		return $this->M->update($this->table, $data,$where);
	}
	public function delete($where){
		return $this->M->delete($this->table, $where);
	}
	public function replace($columns, $search = null, $replace = null, $where = null)
	{
		return $this->M->replace($this->table, $columns,$search,$replace,$where);
	}
	public function get($join = null, $column = null, $where = null)
	{
		return $this->M->get($this->table,$join,$column,$where);
	}
	public function has($join, $where = null)
	{
		return $this->M->has($this->table,$join,$where);
	}
	public function count($join = null, $column = null, $where = null)
	{
		return $this->M->count($this->table,$join,$column,$where);
	}
	public function max($join = null, $column = null, $where = null)
	{
		return $this->M->max($this->table,$join,$column,$where);
	}
	public function min($join = null, $column = null, $where = null)
	{
		return $this->M->min($this->table,$join,$column,$where);
	}
	public function avg($join = null, $column = null, $where = null)
	{
		return $this->M->avg($this->table,$join,$column,$where);
	}
	public function sum($join,$column = null, $where = null){
		return $this->M->sum($this->table, $join,$column, $where);
	}	
	public function error()
	{
		return $this->M->error();
	}	
	public function last_query()
	{
		return $this->M->last_query();
	}	
	public function log()
	{
		return $this->M->log();	
	}
	public function info()
	{
		return $this->M->info();
	}
}